var tongfusoft = {};

tongfusoft.common = function() {
}

tongfusoft.common.prototype = {

	getContextPath : function(locate) {
		var _ctx = $("meta[name='_ctx']").attr("content");
		return document.location.protocol + "//" + document.location.hostname + ":" + document.location.port + _ctx
				+ "/" + locate;

	},
	download_file : function() {

		if (typeof (download_file.iframe) == "undefined") {
			var iframe = document.createElement("iframe");
			download_file.iframe = iframe;
			document.body.appendChild(download_file.iframe);
		}

		download_file.iframe.src = url;
		download_file.iframe.style.display = "none";

	},
	getSmpFormatDateByLong : function(l, isFull) {
		if (l == '' || l == null) {

			return '';
		}
		var pattern = "";
		if (isFull == true || isFull == undefined) {
			pattern = "yyyy-MM-dd hh:mm:ss";
		} else {
			pattern = "yyyy-MM-dd";
		}
		return new Date(l).format(pattern);

	},
	post : function(url, param, func) {
		$.ajax({
			url : this.getContextPath(url),
			type : "post",
			data : param,
			dataType : "json",
			async : false,
			success : function(data) {
				func(data)
			}
		});

	},
	getMenu : function(url, param, func) {
		this.post(url, param, func);
	},
	initDate : function(dateId) {
		if (typeof (dateId) == 'undefined') {
			dateId = "datetimepicker";
		}

		// 日期插入
		$('#' + dateId).datetimepicker({
			icons : {
				time : 'fa fa-clock-o',
				date : 'fa fa-calendar',
				up : 'fa fa-chevron-up',
				down : 'fa fa-chevron-down',
				previous : 'fa fa-chevron-left',
				next : 'fa fa-chevron-right',
				today : 'fa fa-crosshairs',
				clear : 'fa fa-trash'
			}
		});

	},
	getData : function(eId) {
		var inData={};
		$("#" + eId).find("input").each(function() {
			if ($(this).attr("real-value") != null) {
				inData[$(this).attr("name")] = $(this).attr("real-value").trim();
			} else {
				inData[$(this).attr("name")] = $(this).val().trim();
			}
		});

		return inData;

	},
	getFormData : function(eId) {
		var inData={};
		$("#" + eId).find("input").each(function() {
			if ($(this).attr("real-value") != null) {
				inData[$(this).attr("name")] = $(this).attr("real-value").trim();
			} else {
				inData[$(this).attr("name")] = $(this).val().trim();
			}
		});

		$("#" + eId).find("select").each(function() {
			inData[$(this).attr("name")] = $(this).val();
		});
		$("#" + eId).find("textarea").each(function() {
			inData[$(this).attr("name")] = $(this).val().trim();
		});
		return inData;

	},
	getFormDataByClass : function(eId) {
		var inData={};
		$("." + eId).find("input").each(function() {
			if ($(this).attr("real-value") != null) {
				inData[$(this).attr("name")] = $(this).attr("real-value").trim();
			} else {
				inData[$(this).attr("name")] = $(this).val().trim();
			}
		});

		$("." + eId).find("select").each(function() {
			inData[$(this).attr("name")] = $(this).val();
		});
		$("." + eId).find("textarea").each(function() {
			inData[$(this).attr("name")] = $(this).val().trim();
		});
		return inData;

	},
	clean : function(eId) {

		$("#" + eId).find("input").each(function() {
			$(this).val('');
		});

	},
	ajaxSubmitOnSuccess:function(inData,url,sucFunc,opType){
		$.ajax({
			url : this.getContextPath(url),
			data : inData,
			type : 'POST',
			dataType : 'json',
			async : false,
			timeout : 100000,
			error : function() {
				toastr["error"]('通讯错误');
			},
			success : function(data) {
				if (data.state=='success') {
					if(opType == 'del'){
						toastr["success"]("删除成功");
					}else if(opType == 'add' || opType == 'edit'){
						toastr["success"]("保存成功");
					}
					
					if(typeof(sucFunc)=="function"){
						sucFunc(data);
					}
				} else if(data.code!=''||data.code!=null){
					if(typeof(sucFunc)=="function"){
						sucFunc(data);
					}
					
				} else {
					toastr["error"](data.message);
				}
			}
		});
	}

}
tongfusoft.table = function() {
};

tongfusoft.table.prototype = {
	/*
	 * tableId:HTML table id属性 url:获取数据controller url columns: 返回数据JSON 中key
	 * 对应表格html th 的顺序 columnDefs:目前用来隐藏ID 列 [ { "targets" : [ 0 ], "visible" :
	 * false, "searchable" : false }]
	 * 
	 */
	dataTable : function(tableId, url, columns, columnDefs) {

		if (columnDefs == null) {

			columnDefs = [ {
				"targets" : [ 0 ],
				"visible" : false,
				"searchable" : false
			} ]
		}
		$('#' + tableId).DataTable(
				{
					select : {
						style : 'single',
						info : false
					},
					"bSort" : false,
					"processing" : true,
					"serverSide" : true,
					"bStateSave" : true,
					"searching" : false,
					"bLengthChange" : false,

					"iDisplayLength" : 15,
					"ajax" : {
						"url" : common.getContextPath(url),
						"type" : "POST",
						data:function(d){
							
							  return $.extend( {}, d, {
							        "extra_search":JSON.stringify( common.getFormData(tableId+"_search") )
							      } );
							 
						}
					},
					"columns" : columns,

					"columnDefs" : columnDefs,
					"oLanguage" : {
						"sProcessing" : '<span style="color:#ff0000;"><img src="'
								+ common.getContextPath("img/loading.gif") + '"></span>',
						"sLengthMenu" : "每页显示 _MENU_条",
						"sZeroRecords" : "没有找到符合条件的数据",
						"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
						"sInfoEmpty" : "没有记录",
						"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
						"sSearch" : "搜索：",
						"oPaginate" : {
							"sFirst" : "首页",
							"sPrevious" : "前一页",
							"sNext" : "后一页",
							"sLast" : "尾页"
						}
					}

				});

	}

}

/**
 * 根据字典返回显示值
 * 
 * @param cellvalue
 * @param options
 * @param rowObject
 * @returns {String}
 */
function fmtSelect(cellvalue, options, rowObject) {
	// 获取字典的信息
	var formattext = '';
	var obj = dictData[options.colModel.dictCode];
	$(obj).each(function() {
		if (this.value == cellvalue) {
			formattext = this.displayName;
		}
	});

	return formattext;
}

// 删除的格式
function fmtdel(cellValue, options, rowData) {
	if (cellValue != "" && cellValue != null) {
		return "<a href=\"javascript:void(0)\" onclick='delRow(\"" + cellValue
				+ "\")'><i title='删除'class=\"fa fa-trash\"></i></a>";
	} else {
		return '';
	}
}

Date.prototype.format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	// 毫秒
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}
var common = new tongfusoft.common();